//
//  ZZQLocationManager.m
//  zzqKanBoKe
//
//  Created by ben on 17/2/16.
//  Copyright © 2017年 张增强. All rights reserved.
//

#import "ZZQLocationManager.h"
#import <CoreLocation/CoreLocation.h>


@interface ZZQLocationManager ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager * locManager;
@property (nonatomic, copy)LocationBlock block;
@end

@implementation ZZQLocationManager

+ (instancetype)sharedManager{
    static ZZQLocationManager * _manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[ZZQLocationManager alloc] init];
    });
    
    return _manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _locManager = [[CLLocationManager alloc] init];
        [_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
        _locManager.distanceFilter = 100;
        _locManager.delegate = self;
        if(![CLLocationManager locationServicesEnabled]){
            NSLog(@"开启服务");
            
        }else{
            CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
            if(status == kCLAuthorizationStatusNotDetermined){
                [_locManager requestWhenInUseAuthorization];
            }
        }
    }
    return self;
}


- (void)getGps:(LocationBlock)block{
    self.block = block;
    [self.locManager startUpdatingLocation];
}
//代理获得定位信息
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    
    CLLocationCoordinate2D coor = newLocation.coordinate;
    NSString * lat = [NSString stringWithFormat:@"%f", coor.latitude];
    NSString * lon = [NSString stringWithFormat:@"%f", coor.longitude];
    [ZZQLocationManager sharedManager].lat = lat;
    [ZZQLocationManager sharedManager].lon = lon;
    self.block(lat, lon);
    [self.locManager stopUpdatingLocation];
    
}



@end
